import json


class AtmSystem:
    def __init__(self):
        self.__user = None
        self.__password = None

    def register(self):  # 注册，需存入磁盘，用户名，密码，手机号，余额
        # 这里相对路径比较好点
        with open("C:/Users/Administrator/Desktop/homework4/users_info.json") as f:
            d = json.load(f)
            while True:
                username = input("请输入你的用户名：\n")
                if d.get(username) != None:
                    print("用户名已存在，换一个")
                else:
                    break
            password = input("请输入你的密码：\n")
            phone = input("请输入你的手机号：\n")
            d[username] = {"username": username,
                           "password": password, "phone": phone, "money": 5000}
        with open("C:/Users/Administrator/Desktop/homework4/users_info.json", "w") as f:
            json.dump(d, f)
        print("注册成功")
        return self.login()

    def login(self):  # 登录
        # global __user
        self.__user = input("请输入你的用户名：\n")
        # 这里相对路径比较好点
        with open("C:/Users/Administrator/Desktop/homework4/users_info.json") as f:
            x = json.load(f)
            if self.__user not in x.keys():
                print("笨蛋你还没注册，先注册去")
                return self.register()
            else:
                flag = 3
                while flag:
                    self.__password = input("请输入你的密码：\n")
                    if self.__password != x[self.__user]["password"]:
                        print("密码错误")
                        flag -= 1
                    else:
                        self.menu()
                        break
                if flag == 0:
                    print("密码连续输错三次，系统自动退出")

    def menu(self):  # 菜单，输入序号，选择对应功能
        while True:
            num = input("1.查询余额，2.存款，3.取款，4.转账，5.退出系统，请输入你要操作的序号：\n")
            if int(num) == 1:
                self.remainding()
                break
            elif int(num) == 2:
                self.deposit()
                break
            elif int(num) == 3:
                self.draw()
                break
            elif int(num) == 4:
                self.transfer()
                break
            elif int(num) == 5:
                print("系统退出")
                break
            else:
                print("输入有误")

    def remainding(self):  # 查询余额
        with open("C:/Users/Administrator/Desktop/homework4/users_info.json") as f:
            x = json.load(f)
            __money = x[self.__user]["money"]
            print("你的余额是{}".format(__money))
            self.menu()

    def deposit(self):  # 存款
        while True:
            a = input("你要存多少钱，请输入一百的倍数：\n")
            if int(a) % 100 != 0:
                print("只能存入一百的倍数")
            else:
                b = input("是否确定存入{}，1.确定，2.返回 \n".format(a))
                if b == "1":
                    print("好的，为你存入{}".format(a))
                    with open("C:/Users/Administrator/Desktop/homework4/users_info.json") as f:
                        x = json.load(f)
                        x[self.__user]["money"] += int(a)
                        print("你当前余额为{} \n".format(x[self.__user]["money"]))
                        self.menu()
                        break

    def draw(self):  # 取款
        with open("C:/Users/Administrator/Desktop/homework4/users_info.json") as f:
            x = json.load(f)
            while True:
                a = input("你要取多少钱？ \n")
                if x[self.__user]["money"] < int(a):
                    print("兄嘚，你没有那么多钱")
                else:
                    b = input("你确定要取{}吗？1.确定，2.返回 \n".format(a))
                    if b == "1":
                        print("好的，为你取出{} \n".format(a))
                        x[self.__user]["money"] -= int(a)
                        print("你的当前余额为{} \n".format(x[self.__user]["money"]))
                        self.menu()
                        break

    def transfer(self):  # 转账
        with open("C:/Users/Administrator/Desktop/homework4/users_info.json") as f:
            x = json.load(f)
            while True:
                a = input("请输入你要转入的账户名称：\n")
                if a in x.keys():
                    c = input("请输入你的转账金额：\n")
                    if x[self.__user]["money"] < int(c):
                        print("兄嘚，你没有那么多钱")
                    else:
                        print("好的，为{}转入{}". format(a, c))
                        x[self.__user]["money"] -= int(c)
                        x[a]["money"] += int(c)
                        self.menu()
                        break
                else:
                    b = input("你输入用用户名不存在，请确定后输入,1.继续，2.返回菜单 \n")
                    if b == "2":
                        self.menu()
                        break
    def start(self):
        while True:
            x = input("1.注册，2.登录，请输入你想操作的序号：\n")
            if int(x) == 1:
                self.register()
                break
            elif int(x) == 2:
                self.login()
                break
            else:
                print("我们再来一遍")


pp = AtmSystem()
pp.start()
# 逻辑上没有啥问题，比如路径这个地方还是优化成一个类方法的
